package com.neusoft.elmboot.service.Impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.elmboot.mapper.CreditTransMapper;
import com.neusoft.elmboot.po.CreditTime;
import com.neusoft.elmboot.po.CreditTrans;
import com.neusoft.elmboot.service.CreditService;
import com.neusoft.elmboot.util.CommonUtil;

@Service
public class CreditServiceImpl implements CreditService{
	
	@Autowired
	private CreditTransMapper creditTransMapper;
	
	@Override
	public int addCredit(CreditTrans creditTrans) {
		creditTrans.setChannelId(1);
		creditTrans.setCreateTime(CommonUtil.getCurrentDate());
		
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(creditTrans.getCreateTime(), formatter);
        LocalDateTime newDateTimeAdd = dateTime.plusMonths(CreditTime.timeM);  
        String resultAdd = newDateTimeAdd.format(formatter);

        creditTrans.setExpiredTime(resultAdd);
        
        creditTrans.setLeftVal(creditTrans.getCredit());
        
        creditTransMapper.addCredit(creditTrans);
        return creditTrans.getId();
	}
	
	@Override
	public int updateCredit(String userId) {
		int cnt = 0;
		String time = CommonUtil.getCurrentDate();
		// 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将时间字符串转换为 LocalDateTime 对象
        LocalDateTime dateTime1 = LocalDateTime.parse(time, formatter);
        
		List<CreditTrans> listTrans = creditTransMapper.getCredit(userId);	
		for(CreditTrans c : listTrans) {
			LocalDateTime dateTime2 = LocalDateTime.parse(c.getExpiredTime(), formatter);
			if(dateTime1.isAfter(dateTime2)) {
				creditTransMapper.invalid(c.getId());
				cnt++;
			}
				
		}
		return cnt;
	}
	
	@Override
	public List<CreditTrans> getCreditTrans(String userId){
		updateCredit(userId);
		return creditTransMapper.getCredit(userId);
	}
	
	@Override
	public int getCredit(String userId) {
		int credit = 0;
		updateCredit(userId);
		List<CreditTrans> listTrans = creditTransMapper.getCredit(userId);
		for(CreditTrans c : listTrans) {
			credit += c.getLeftVal();
		}
		return credit;
	}
	
	@Override
	public List<CreditTrans> getaddingTrans(String userId){
		updateCredit(userId);
		return creditTransMapper.getaddingTrans(userId);
	}
	
	@Override
	public List<CreditTrans> getusingTrans(String userId){
		return creditTransMapper.getusingTrans(userId);
	}
	
	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ)
	public int usingCredit(CreditTrans creditTrans) {
		if(getCredit(creditTrans.getUserId()) < creditTrans.getCredit()) return 0;
		//产生记录
		creditTrans.setChannelId(0);
		creditTrans.setCreateTime(CommonUtil.getCurrentDate());
		creditTrans.setExpiredTime(CommonUtil.getCurrentDate());
		creditTrans.setLeftVal(0);
		creditTransMapper.addCredit(creditTrans);
		//使用积分
		int need = creditTrans.getCredit();
			//筛选取得积分记录 正序
		updateCredit(creditTrans.getUserId());
		List<CreditTrans> listTrans = creditTransMapper.getadding(creditTrans.getUserId());
		for(CreditTrans c : listTrans) {
			if(c.getLeftVal() > 0) {
				//剩余小于需要
				if(c.getLeftVal() < need) {
					need-=c.getLeftVal();
					creditTransMapper.invalid(c.getId());
					continue;
				}
				//剩余大于等于需要
				else {
					creditTransMapper.setleft(c.getId(), need);
					break;
				}
			}
		}
        return creditTrans.getId();
	}
	
}